package com.ruoyi.socketServer;


import com.alibaba.fastjson.JSON;
import com.ruoyi.system.buffer.GameData;
import com.ruoyi.web.controller.Game.EventDeliveryManager;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Component
public class NettyServerHandler extends SimpleChannelInboundHandler<Object> {

    private static final Logger log = LoggerFactory.getLogger(NettyServerHandler.class);

    private Map<String,SocketChannel> channnels=new HashMap<String,SocketChannel>();

    @Autowired
    EventDeliveryManager testEvent=new EventDeliveryManager();

    /**
     * 读取数据
     * @param context
     * @param obj
     * @throws Exception
     */
    protected void channelRead0(ChannelHandlerContext context, Object obj) throws Exception {
        log.info(">>>>>>>>>>>服务端接收到客户端的消息：{}",obj);
        SocketChannel socketChannel = (SocketChannel) context.channel();
        GameData resquest=new GameData();
        resquest= JSON.parseObject(obj.toString(),GameData.class);
        testEvent.RequestManager(resquest,socketChannel);
    }



    /**
     * 读取完毕
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }

}
